package com.ld.spring.source.config;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.pattern.color.ANSIConstants;
import ch.qos.logback.core.pattern.color.ForegroundCompositeConverterBase;

/**
 * @author littlelot
 */
public class LogbackColorful extends ForegroundCompositeConverterBase<ILoggingEvent> {
    @Override
    protected String getForegroundColorCode(ILoggingEvent event) {
        Level level = event.getLevel();
        switch (level.toInt()) {
            // ERROR 等级为红色
            case Level.ERROR_INT:
                return ANSIConstants.BOLD + ANSIConstants.RED_FG;
                // WARN 等级为黄色
            case Level.WARN_INT:
                return ANSIConstants.BOLD + ANSIConstants.YELLOW_FG;
                // INFO 等级为蓝色
            case Level.INFO_INT:
                return ANSIConstants.BOLD + ANSIConstants.BLUE_FG;
                // DEBUG 等级为绿色
            case Level.DEBUG_INT:
                return ANSIConstants.BOLD + ANSIConstants.GREEN_FG;
                // 其他为默认颜色
            default:
                return ANSIConstants.DEFAULT_FG;
        }
    }
}
